// 创建action
import Types from '../actionTypes';

import {
  getMaterialGroupData,
  getMaterialGroupInfo,
  addMaterialGroup,
  editMaterialGroup,
  delMaterialGroup, getInstructionCount
} from '../../api';

// 记录select_key的值
const SELECT_KEY = value => {
  return {
    type: Types.SELECT_KEY,
    value
  };
};

// 初始化数据
const INIT_DATA = () => {
  return new Promise((resolve, reject) => {
    try {
      getMaterialGroupData()
        .then(res => {
          resolve({
            type: Types.MARTERIAL_GROUP_DATA,
            value: res.data
          });
        })
        .catch(err => {
          console.log(err);
        });
    } catch (err) {
      console.log(err);
    }
  });
};

// 查看该物料组信息
const GET_MAMARTERIAL_GROUP_INFO = id => {
  return new Promise((resolve, reject) => {
    getMaterialGroupInfo(id)
      .then(res => {
        resolve({
          type: Types.GET_MAMARTERIAL_GROUP_INFO,
          value: res.data
        });
      })
      .catch(err => {
        console.log(err);
      });
  });
};

// 增加
const ADD_DATA = data => {
  console.log(data);
  let pid = data.pid[0].split(',')[1];
  let temp = {
    cate_code: data.cate_code,
    cate_title: data.cate_title,
    pid
  };
  return new Promise((resolve, reject) => {
    addMaterialGroup(temp).then(res => {
      // 更新视图
      getMaterialGroupData()
        .then(res => {
          resolve({
            type: Types.MARTERIAL_GROUP_DATA,
            value: res.data
          });
        })
        .catch(err => {
          console.log(err);
        });
    }).catch(err => {
      console.log(err);
    });
  });
};

// 编辑
const EDIT_DATA = data => {
  return new Promise((resolve, reject) => {
    let temp = {
      cate_title: data.cate_title
      // cate_code: data.cate_code
    };
    let id = data.pid[0].split(',')[1];
    let myId = data.pid[0].split(',')[0];
    console.log(myId);
    editMaterialGroup(temp, myId)
      .then(res => {
        // 更新视图
        Promise.resolve([getMaterialGroupData(), getMaterialGroupInfo(myId)]).then(res => {
          res[0].then(res1 => {
            resolve({
              type: Types.MARTERIAL_GROUP_DATA,
              value: res1.data
            });
          });
          res[1].then(res2 => {
            resolve({
              type: Types.GET_MAMARTERIAL_GROUP_INFO,
              value: res2.data
            });
          });
        }).catch(err => {
          console.log(err);
        });
      })
      .catch(err => {
        console.log(err);
      });
  });
};

// 删除
const DEL_DATA = id => {
  let tempID = id[0].split(',')[0];

  return new Promise((resolve, reject) => {
    delMaterialGroup(tempID)
      .then(res => {
        // 更新视图
        getMaterialGroupData()
          .then(res => {
            resolve({
              type: Types.MARTERIAL_GROUP_DATA,
              value: res.data
            });
            window.location.reload();
          })
          .catch(err => {
            console.log(err);
          });
      })
      .catch(err => {
        console.log(err);
      });
  });
};

export default {
  SELECT_KEY,
  GET_MAMARTERIAL_GROUP_INFO,
  INIT_DATA,
  ADD_DATA,
  EDIT_DATA,
  DEL_DATA
};
